//
//  LYOrderDetailViewController.m
//  artist
//
//  Created by Jay_Apple on 16/4/8.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYOrderDetailViewController.h"
#import "LYOrderDetailModel.h"
#import "VVYPayManager.h"
#import "LYPayFinshViewController.h"

@interface LYOrderDetailViewController ()

@property (nonatomic, strong) LYOrderDetailModel *model;
@property (weak, nonatomic) IBOutlet UILabel *className;

@property (weak, nonatomic) IBOutlet UILabel *address;
@property (weak, nonatomic) IBOutlet UILabel *price;
//支付宝支付按钮
@property (weak, nonatomic) IBOutlet UIButton *alipayBtn;

//当前支付方式
@property (assign, nonatomic) PayType type;
//选择的支付按钮
@property (weak, nonatomic) UIButton *selectedBtn;

@end

@implementation LYOrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 初始化
- (void)initData{
    
    self.navigationItem.title = @"提交订单";
    [self selectPayType:self.alipayBtn];
    [self setModel:self.model];
    [self getOrderDetailData];
}


#pragma mark - 重写

- (void)setModel:(LYOrderDetailModel *)model{

    _model = model;
    
    self.className.text = [NSString stringWithFormat:@"课程名称：%@",_model.title];
    
    //地址
    NSString *address = [NSString stringWithFormat:@"上课地点：%@%@%@%@",_model.course_info.province,_model.course_info.city,_model.course_info.area,_model.course_info.address];
    self.address.text = address;
    //价钱
    self.price.text = [NSString stringWithFormat:@"¥%@",_model.order_money];
}


#pragma mark - 网络请求
- (void)getOrderDetailData{
    
    [MBProgressHUD showMessage:nil];
    [LYRequestTool orderDetailWithToken:LYToken order_sn:self.order_sn success:^(LYOrderDetailModel *model) {
        [MBProgressHUD hideHUD];
        self.model = model;
        
    } failure:^(NSString *code, NSString *message) {
        [MBProgressHUD hideHUD];
    }];

}


#pragma mark - 按钮点击
- (IBAction)payBtnOnclick:(id)sender {
    
    [[VVYPayManager sharedManager] payWithOrderNum:self.model.order_sn price:self.model.order_money andPayType:self.type success:^{
        [MBProgressHUD showSuccess:@"支付成功"];
        [self.navigationController popViewControllerAnimated:NO];
        [[NSNotificationCenter defaultCenter] postNotificationName:LYPaySuccessNotification object:nil];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            LYPayFinshViewController *finishedVC = [[LYPayFinshViewController alloc] init];
            finishedVC.order_sn = self.order_sn;
            UITabBarController *tabbarVC = [UIApplication sharedApplication].keyWindow.rootViewController;
            UINavigationController *nav = tabbarVC.selectedViewController;
            [nav pushViewController:finishedVC animated:YES];

        });
        
    } failure:^(NSString *error) {
        
        [MBProgressHUD showError:error];

    }];
    
}

/**
 *  选择支付方式
 */
- (IBAction)selectPayType:(UIButton *)sender {
    
    self.selectedBtn.selected = NO;
    sender.selected = YES;
    self.selectedBtn = sender;
    self.type = sender.tag;
}
@end
